"""
--------------------------------------
FileName: work.1.py
Content: 
        ch2.work1算法的正确性

Date:    25/09/15
Auther:  杨则瑞
Encode:  utf-8 | CRLF
--------------------------------------
"""



import csv
import subprocess
import io
import os
import sys
import random

from math import sqrt
from math import floor

failCnt = 0



def generate_test_case():

    num  = random.randint(700,10000)
    
    tst = []
    for i in range(0, num):
        tst.append(random.randint(-2147483648, 2147483647))

    a = random.randint(-2147483648, 2147483647)
    b = random.randint(a, 2147483647)

    

    def get_correct_result(a : list[int], mi, mx) -> list[int]:
        outp = []
        for i in a:
            if not (mi < i < mx):
                outp.append(i)
        return outp


    outpt =  get_correct_result(tst, a, b)

    # Fmting
    inpt = bytes()
    inpt += (bytes(str(num) + '\n',encoding="utf-8"))
    inpt += (bytes(str(a) + '\n',encoding="ascii"))
    inpt += (bytes(str(b) + '\n',encoding="ascii"))


    for i in tst:
        inpt += (bytes(str(i) + '\n',encoding="ascii"))

  

    return inpt, outpt







def get_shorter(s):
    return "{}...{}".format(s[:10],s[-10:])

for case in range(0,10):

    sys.stdout.write("Testing case %d " % case)
    
    target = (sys.argv[1])

    handler = subprocess.Popen(target,
                           stdin  = subprocess.PIPE,
                           stdout = subprocess.PIPE,
                           stderr = subprocess.STDOUT)

    
    if not handler.stdin.writable:

        print("Communication Error.")
        exit(1)
    if not handler.stdout.readable:
        print("Communication Error.")
        exit(2)

    flag : bool = True
    



    inpt , oue = generate_test_case() 


    # 获取输出
    out, _ = handler.communicate(inpt)
    out = out.decode("ascii")
    out = out.split('\n')
    our = eval(out[1])

    sys.stdout.write("\nJudging if {} equals to {} ".format(get_shorter(str(our)), get_shorter(str(oue))))
    # 判断相等
    flag = (our == oue)
    

    if(flag == True):
        print("\033[1;32m""Pass""\033[0;10m")
    else:
        failCnt += 1
        print("\033[1;31m""Fail""\033[0;10m") 
                
    
print("Result:")

if failCnt == 0:
    print("\033[1;32m""All Pass.""\033[0;10m")
else :
    print("\033[1;10m""{} Fails cases".format(failCnt))